package com.butang.Dao;


import java.sql.*;

/**
 * @Author weizs
 * @Create 2023/6/7 16:13
 * @Version 1.0
 */
public class conf {


    //十六位二进制最大数值
    static int HexBinary_Max = 65535;
    static long DoubleHexBinary_Max = 4294967295L;

    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        long startRead = 0;
        long endRead = 0;
        long endAnalysis = 0;
        long endInsert = 0;

        int count = 0;
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/scada", "root", "root");
            PreparedStatement pstmt = conn.prepareStatement("select * from meta_tag");
            Statement statement = conn.createStatement();

            String sql = null;
            ResultSet rs = null;

            System.out.println("------------------------开始解析:");

            startRead = System.currentTimeMillis();
            sql = "select a.TagID,a.miniMum,a.maxiMum,b.TagValue,a.Address,a.Description,a.Conf from meta_tag a inner join meta_tag_real_data b on a.Address=b.Address";
            endRead = System.currentTimeMillis();

            PreparedStatement preparedStatement = null;

            rs = statement.executeQuery(sql);


            while (rs.next()) {
                count++;
                int TagID = rs.getInt("TagID");
                int maxiMum = rs.getInt("maxiMum");
                int miniMum = rs.getInt("miniMum");
                String description = rs.getString("Description");

                int i1;

                int i2;
                if (description.equals(32)) {
                    i1 = 2;
                } else {
                    i1 = 1;
                }


                if (maxiMum == 1 | maxiMum == 0) i2 = 1;
                else if (maxiMum ==  10) i2 = 2;
                else if (maxiMum == 100) i2 = 3;
                else {i2 = 4;}

                Integer conf = i1*100 + i2*10 + miniMum;



                String updateSql = "update meta_tag set Conf=? where TagID=?";

                preparedStatement = conn.prepareStatement(updateSql);
                preparedStatement.setInt(1, conf);
                preparedStatement.setInt(2, TagID);
                preparedStatement.executeUpdate();
                //把data存储到容器中，然后一次性插入
               // preparedStatement.addBatch();

              //  System.out.println(preparedStatement);


            }


            endAnalysis = System.currentTimeMillis();

            System.out.println("hello");
          //  int result = preparedStatement.executeUpdate();
            //System.out.println("result"+result);
            endInsert = System.currentTimeMillis();

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

        System.out.println("读取耗时：" + (endRead - startRead));
        System.out.println("解析耗时" + (endAnalysis - endRead));
        System.out.println("插入耗时" + (endInsert - endAnalysis));

        long end = System.currentTimeMillis();
        System.out.println("共解析" + count + "条" + '\t' + "共耗时:" + (end - start));


    }

}